package org.game.utils;

import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * 时间相关的工具
 * @author fangming
 * 2015年8月6日
 */
public class DateUtil {

	public static final String PATTERN_HH_MM = "HH:mm";
	public static final String PATTERN_YYYYMMDD = "yyyyMMdd";
	public static final String PATTERN_YYYY_MM_DD = "yyyy-MM-dd";
	public static final String PATTERN_YYYYMMDDHH = "yyyyMMddHH";
	public static final String PATTERN_YYYYMMDDHHMM = "yyyyMMddHHmm";
	public static final String PATTERN_NORMAL = "yyyy-MM-dd HH:mm:ss";
	public static final String PATTERN_YYYYMMDDHHMMSS = "yyyyMMddHHmmss";
	
	/**
	 * 将日期转化成指定格式的字符串
	 * 
	 * @param theDate
	 * @param datePattern
	 * @return
	 */
	public static String date2String(Date theDate, String datePattern) {
		if (theDate == null) {
			return "";
		}
		DateFormat format = new SimpleDateFormat(datePattern);
		try {
			return format.format(theDate);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return "";
	}

	/**
	 * 将时间字符串根据指定的时间格式转化成时间对象
	 * @param dateString
	 * @param datePattern
	 * @return
	 */
	public static Date string2Date(String dateString, String datePattern) {
		if ((dateString == null) || (dateString.trim().isEmpty())) {
			return null;
		}
		DateFormat format = new SimpleDateFormat(datePattern);
		try {
			return format.parse(dateString);
		} catch (ParseException e) {
			e.printStackTrace();
		}
		return null;
	}
	
	
	
}
